<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree方主体内容 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.lengsh === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
      <!-- 放置弹层区域 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '新增部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        width="50%"
        @close="resetForm"
      >
        <!-- 解决数据不更新的问题,方法1   给子组件添加 v-if="showDialog" 销毁这个子组件
        缺点 : 每次执行就会销毁一次,影响加载速度-->
        <addOrEdit
          :id="curId"
          ref="addOrEditRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @transmit="hTransmit"
          @close="showDialog= false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartmentsEte } from '@/api/departments'
import { departmentsTree } from '@/utils'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false,
      // 接收res.data里面的数据
      list: [],
      curId: '',
      isEdit: false,
      // 保存所有部门列表的部分(id,name,pid,code) 数据
      originList: []
    }
  },

  created() {
    this.loadDepartments()
  },
  methods: {
    // 清空表单
    resetForm() {
      // 通过在父组件中调用子组件的重置方法,resetForm实现重置表单
      this.$refs.addOrEditRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确认要删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定删除
        await delDepartmentsEte(id)
        // 重新渲染页面
        this.loadDepartments()
        this.$message.error('恭喜您删除成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    hTransmit() {
      this.loadDepartments()
      this.showDialog = false
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // dom更新数异步的
      // 方法二 : 解决数据不更新的问题
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentsDetail()
      })
    },
    hAdd(id) {
      this.curId = id
      // 打开弹层
      this.isEdit = false
      // console.log(id, 'idididid')
      this.showDialog = true
    },
    async  loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('res', res)
        // 获取部门数据并取出 , 数组中的每个对象部门数据{id , pid , code , name}
        this.originList = res.data.depts.map(item => {
          return { id: item.id, name: item.name, pid: item.pid, code: item.code }
        })
        // 删除数组中第一条数据,取出公司的名称
        res.data.depts.shift()
        this.list = departmentsTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
